package com.ruoyi.backend.service.impl;

import java.util.List;

import com.ruoyi.backend.constant.CommonConstant;
import com.ruoyi.backend.domain.dto.OrderDto;
import com.ruoyi.backend.enums.OrderChangeTypeEnum;
import com.ruoyi.backend.enums.OrderStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.backend.mapper.OrderMapper;
import com.ruoyi.backend.domain.Order;
import com.ruoyi.backend.service.IOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author wjr
 * @date 2025-09-23
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(Integer id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param orderDto 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(OrderDto orderDto)
    {
        return orderMapper.selectOrderList(orderDto);
    }

    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateBy(SecurityUtils.getUsername());
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateBy(SecurityUtils.getUsername());
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Integer[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Integer id)
    {
        return orderMapper.deleteOrderById(id);
    }

    /**
     * 修改订单
     *
     * @param orderDto 订单
     * @return 结果
     */
    @Override
    public int changeOrder(OrderDto orderDto)
    {
        //根据不同的参数，将订单设置为不同的状态
        if (OrderChangeTypeEnum.SHOP_ALLOCATION_CHARGE.getCode().equals(orderDto.getChangeType())) {
            //此时为分配负责人，需要将订单状态设置为待开始（店铺分配负责人）
            orderDto.setOrderStatus(OrderStatusEnum.WAIT_START2.getCode());
        } else if (OrderChangeTypeEnum.CUSTOMER_CONFIRM_PERFORM.getCode().equals(orderDto.getChangeType())) {
            //此时为确认进行中，需要将订单状态设置为进行中（客户确认进行中）
            orderDto.setOrderStatus(OrderStatusEnum.IN_PROGRESS.getCode());
        } else if (OrderChangeTypeEnum.SHOP_SUBMIT_COMPLETE.getCode().equals(orderDto.getChangeType())) {
            //此时为店铺提交已完成，需要将订单状态设置为待确认（店铺提交已完成）
            orderDto.setOrderStatus(OrderStatusEnum.COMPLETE1.getCode());
        } else if (OrderChangeTypeEnum.CUSTOMER_CONFIRM_COMPLETE.getCode().equals(orderDto.getChangeType())) {
            //此时为客户确认已完成，同时付尾款
            orderDto.setOrderStatus(OrderStatusEnum.COMPLETE2.getCode());
        } else if (OrderChangeTypeEnum.SHOP_CONFIRM_SETTLE_ACCOUNTS.getCode().equals(orderDto.getChangeType())) {
            //此时为店铺确认收到尾款，需要将订单状态设置为已完成（店铺确认收到尾款）
            orderDto.setOrderStatus(OrderStatusEnum.SETTLED.getCode());
        }
        orderDto.setUpdateBy(SecurityUtils.getUsername());
        orderDto.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.changeOrder(orderDto);
    }

}
